package Rechner;
//Importieren von den benötigten Bibliotheken
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

//durch "extends" kann man auf vorhandenen Methoden zurueckgreifen
public class GUI extends JFrame {

	private static final long serialVersionUID = -7298368398164263519L;
	//Deklarierung der entsprecheneden Felder, verschiedene Arten
	JScrollPane historyScrollField;
	JTextPane historyField;
	JFormattedTextField firstInputField;
	JLabel placeholderLeft;
	JTextField operatorField;
	JFormattedTextField secondInputField;
	Font defaultFont;
	JPanel standardPanel;
	JPanel specialPanel;
	// Deklarierung der Bedienfelder(Buttons)
	CalculatorButton exportButton;
	CalculatorButton divideButton;
	CalculatorButton multiplyButton;
	CalculatorButton backspaceButton;
	CalculatorButton sevenButton;
	CalculatorButton eightButton;
	CalculatorButton nineButton;
	CalculatorButton minusButton;
	CalculatorButton fourButton;
	CalculatorButton fiveButton;
	CalculatorButton sixButton;
	CalculatorButton plusButton;
	CalculatorButton oneButton;
	CalculatorButton twoButton;
	CalculatorButton threeButton;
	CalculatorButton moduloButton;
	CalculatorButton dotButton;
	CalculatorButton plusMinusButton;
	CalculatorButton calculateButton;
	CalculatorButton zeroButton;
	CalculatorButton clearButton;

	public GUI() {
		// Größe des Fensters, so wie es aufgeht
		setSize(680, 400);
		// damit sich das Fenster schließt wenn man auf das "x" klickt
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// Positionierung auf dem Bildschirm
		setLocationRelativeTo(null);
		// Titel des Fensters
		setTitle("Taschenrechner");
		setResizable(true);
		// gibt Schriftart, Schriftgröße an
		defaultFont = new Font("Verdana", Font.BOLD + Font.ITALIC, 15); 
		
		//legt ein neues Panel fest
		standardPanel = new JPanel();
		standardPanel.setLayout(new GridBagLayout());
		// fügt das Panel dem der GUI hinzu
		this.add(standardPanel);
		
		//gibt die Größe der Einzelnen Bottons in Pixel an 
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.5;

		// Nun werden die Einzelnen Buttons im Panel angelegt, also auf welche Plätze welcher Button kommt
		// das "this" referenziert die Buttons mit der GUI
		exportButton = new CalculatorButton("save", this);
		c.gridx = 0;
		c.gridy = 0;
		standardPanel.add(exportButton, c);

		divideButton = new CalculatorButton("/", this);
		c.gridx = 1;
		c.gridy = 0;
		standardPanel.add(divideButton, c);

		multiplyButton = new CalculatorButton("*", this);
		c.gridx = 2;
		c.gridy = 0;
		standardPanel.add(multiplyButton, c);

		backspaceButton = new CalculatorButton("<-", this);
		c.gridx = 3;
		c.gridy = 0;
		standardPanel.add(backspaceButton, c);
		
		// gridwidth und gridheight werden geändert um ein größeres Feld zu erzeugen
		c.gridwidth = 3;
		c.gridheight = 2;
		historyField = new JTextPane();
		historyScrollField = new JScrollPane(historyField);
		historyField.setText("                                    Log:\n");
		historyField.setFont(defaultFont);
		
		// folgende 3 Zeilen sind nur für den rechtsbündigen Text
		SimpleAttributeSet historyFieldAttributes = new SimpleAttributeSet();
		StyleConstants.setAlignment(historyFieldAttributes , StyleConstants.ALIGN_RIGHT);
		historyField.setParagraphAttributes(historyFieldAttributes, true);
		
		// Positionierung des "Historyfeldes"
		historyField.setEditable(false);
		c.gridx = 4;
		c.gridy = 0;
		standardPanel.add(historyScrollField, c);
		// Zurücksetzten der ursprünglichen Werte
		c.gridwidth = 1;
		c.gridheight = 1;

		sevenButton = new CalculatorButton("7", this);
		c.gridx = 0;
		c.gridy = 1;
		standardPanel.add(sevenButton, c);

		eightButton = new CalculatorButton("8", this);
		c.gridx = 1;
		c.gridy = 1;
		standardPanel.add(eightButton, c);

		nineButton = new CalculatorButton("9", this);
		c.gridx = 2;
		c.gridy = 1;
		standardPanel.add(nineButton, c);

		minusButton = new CalculatorButton("-", this);
		c.gridx = 3;
		c.gridy = 1;
		standardPanel.add(minusButton, c);
		/*
		 *  Erzeugung eines DecimalFormats, anhand dem dann die eingegebenen Werte evaluiert werden
		 */
		DecimalFormat decimalFormat = new DecimalFormat();
		decimalFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));///
		decimalFormat.setMaximumFractionDigits(2);
		decimalFormat.setMinimumFractionDigits(2);
		decimalFormat.setGroupingUsed(false);
		
		/* 
		 * Änderung der Länge/Breite des Eingabefeldes("firstInputfield")
		 */
		c.gridwidth = 3;
		/*
		 *  Erzeugen des formatierten TextFields unter Verwendung des oben definierten DecimalFormats
		 */
		firstInputField = new JFormattedTextField(decimalFormat);
		firstInputField.setFont(defaultFont);
		firstInputField.setHorizontalAlignment(JTextField.RIGHT);
		firstInputField.setEditable(true);
		firstInputField.setInputVerifier(null);
		c.gridx = 4;
		c.gridy = 2;
		standardPanel.add(firstInputField, c);
		
		/*
		 *  zurücksetzten der Länge/Breite auf 1 für die folgenden GUI-Elemente
		 */
		c.gridwidth = 1;

		fourButton = new CalculatorButton("4", this);
		c.gridx = 0;
		c.gridy = 2;
		standardPanel.add(fourButton, c);

		fiveButton = new CalculatorButton("5", this);
		c.gridx = 1;
		c.gridy = 2;
		standardPanel.add(fiveButton, c);

		sixButton = new CalculatorButton("6", this);
		c.gridx = 2;
		c.gridy = 2;
		standardPanel.add(sixButton, c);

		plusButton = new CalculatorButton("+", this);
		c.gridx = 3;
		c.gridy = 2;
		standardPanel.add(plusButton, c);
		
		placeholderLeft = new JLabel();
		c.gridx = 4;
		c.gridy = 3;
		standardPanel.add(placeholderLeft, c);
		
		operatorField = new JTextField("");
		/*
		 *  Possitionierung
		 */
		operatorField.setHorizontalAlignment(JTextField.CENTER);
		operatorField.setEditable(false);
		c.gridx = 5;
		c.gridy = 3;
		standardPanel.add(operatorField, c);
		
		clearButton = new CalculatorButton("clear", this);
		c.gridx = 6;
		c.gridy = 3;
		standardPanel.add(clearButton, c);
		
		oneButton = new CalculatorButton("1", this);
		c.gridx = 0;
		c.gridy = 3;
		standardPanel.add(oneButton, c);

		twoButton = new CalculatorButton("2", this);
		c.gridx = 1;
		c.gridy = 3;
		standardPanel.add(twoButton, c);

		threeButton = new CalculatorButton("3", this);
		c.gridx = 2;
		c.gridy = 3;
		standardPanel.add(threeButton, c);

		moduloButton = new CalculatorButton("mod", this);
		c.gridx = 3;
		c.gridy = 3;
		standardPanel.add(moduloButton, c);

		c.gridwidth = 3;
		secondInputField = new JFormattedTextField(decimalFormat);
		secondInputField.setFont(defaultFont);
		secondInputField.setHorizontalAlignment(JTextField.RIGHT);
		secondInputField.setEditable(true);
		c.gridx = 4;
		c.gridy = 4;
		standardPanel.add(secondInputField, c);
		c.gridwidth = 1;
		
		zeroButton = new CalculatorButton("0", this);
		c.gridx = 0;
		c.gridy = 4;
		standardPanel.add(zeroButton, c);

		dotButton = new CalculatorButton(".", this);
		c.gridx = 1;
		c.gridy = 4;
		standardPanel.add(dotButton, c);

		plusMinusButton = new CalculatorButton("+/-", this);
		c.gridx = 2;
		c.gridy = 4;
		standardPanel.add(plusMinusButton, c);

		calculateButton = new CalculatorButton("=", this);
		c.gridx = 3;
		c.gridy = 4;
		standardPanel.add(calculateButton, c);

		/*
		 *  ganze GUI sichtbar machen
		 */
		this.setVisible(true);
		/*
		 *  und neu zeichnen, speziell unter Linux und Mac OS X wichtig
		 */
		this.repaint();
	}
}